package com.example.library.util.impl;

import com.example.library.entity.Book;
import com.example.library.util.BookDao;

import javax.servlet.annotation.WebServlet;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static final String DB_URL = "jdbc:mysql://localhost:3306/library?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "root123";
    @Override
    public void addBook(Book book) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)){
            String sql = "insert into books(book_Id,book_Name,author,publisher,publication_Year,price,stock_Quantity) values(?,?,?,?,?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,book.getBookId());
            preparedStatement.setString(2,book.getBookName());
            preparedStatement.setString(3,book.getAuthor());
            preparedStatement.setString(4,book.getPublisher());
            preparedStatement.setInt(5,book.getPublicationYear());
            preparedStatement.setDouble(6,book.getPrice());
            preparedStatement.setInt(7,book.getStockQuantity());
            preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void deleteBook(String bookId) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)){
            String sql = "delete from book where bookId=?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,bookId);
            preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void updateBook(Book book) {
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)){
            String sql = "update book set bookName=?,author=?,publisher=?,publicationYear=?,price=?,stockQuantity=? where bookId=?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,book.getBookName());
            preparedStatement.setString(2,book.getAuthor());
            preparedStatement.setString(3,book.getPublisher());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Book> getBooks(String keyword) {
        List<Book> books=new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)){
            String sql = "SELECT * FROM books WHERE book_name LIKE? OR author LIKE? OR publisher LIKE?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,"%"+keyword+"%");
            preparedStatement.setString(2,"%"+keyword+"%");
            preparedStatement.setString(3,"%"+keyword+"%");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Book book=new Book(
                        resultSet.getString("book_id"),
                        resultSet.getString("book_name"),
                        resultSet.getString("author"),
                        resultSet.getString("publisher"),
                        resultSet.getInt("publication_year"),
                        resultSet.getDouble("price"),
                        resultSet.getInt("stock_quantity")
                );
                books.add(book);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return books;
    }
}
